package cn.com.dhcc.turbo.dao.exch;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPENCLOSE;
import cn.com.dhcc.turbo.entity.exch.GapOwnerConf;
import cn.com.dhcc.turbo.entity.exch.GapUserConf;

@Repository
public class GapOwnerConfDao extends AbstractCrudDao<GapOwnerConf> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
    
	/**
	 * 根据节点的拥有者查询列表
	 * @param owner
	 * @return
	 * @throws DaoException
	 */
	public List<GapOwnerConf> getAllByOwner(String owner) throws DaoException {
		String msg = "";
		try {
			String sql = "select a.*,b.node_name gapOwnerName,b.id gapOwneId,c.node_name gapUserName,c.id gapUserId from "
					+ SI_TABLE(" turbo_gap_owner_conf")+"   a "
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO")+"   b "
					+ " on a.GAP_OWNER=b.node_code"
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO")+"  c "
					+ " on a.GAP_USER=c.node_code"
					+ " where a.GAP_OWNER = ?  and a.status=?";
			return CM.getDao().listModule("根据节点拥有者查询列表", sql, GapOwnerConf.class, new Object[]{owner,OPENCLOSE.OPEN.getStatus()});
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 获取网闸可用信息
	 * @param gapuser
	 * @return
	 * @throws DaoException 
	 */
    public List<GapUserConf> getAllByUserAsUserConfig(String gapuser) throws DaoException{
    	String msg = "";
		try {
			String sql = "select a.*,b.node_name nodeName from "
					+ SI_TABLE(" turbo_gap_owner_conf")+"   a" +
					 " left join  "+
					SI_TABLE(" TURBO_NODE_INFO")+"   b on a.GAP_OWNER=b.node_code  " + 
					 " where a.GAP_User = ? and a.status!='0' ";
			return CM.getDao().listModule("根据用户查询列表", sql, GapUserConf.class, new Object[]{gapuser});
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 根据节点的使用者查询列表
	 * @param gapUser
	 * @return
	 * @throws DaoException
	 */
	public List<GapOwnerConf> getAllByUser(String gapUser) throws DaoException {
		String msg = "";
		try {
			String sql = "select a.*,b.node_name gapOwnerName,b.id gapOwneId,c.node_name gapUserName,c.id gapUserId from "
					+ SI_TABLE(" turbo_gap_owner_conf")+"   a "
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO")+"   b"
					+ " on a.GAP_OWNER=b.node_code"
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO")+"  c"
					+ " on a.GAP_USER=c.node_code"
					+ " where a.GAP_User = ? ";
			return CM.getDao().listModule("根据用户查询列表", sql, GapOwnerConf.class, new Object[]{gapUser});
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 根据节点的使用者查询列表
	 * @param gapUser
	 * @return
	 * @throws DaoException
	 */
	public GapOwnerConf getByUserAndOwner(String gapUser,String owner)  throws DaoException {
		String msg = "";
		try {
			String sql = "select a.*,b.node_name gapOwnerName,b.id gapOwneId,c.node_name gapUserName,c.id gapUserId from "
					+ SI_TABLE(" turbo_gap_owner_conf  a ")
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO  b")
					+ " on a.GAP_OWNER=b.node_code"
					+ " inner join  "
					+ SI_TABLE(" TURBO_NODE_INFO c")
					+ " on a.GAP_USER=c.node_code"
					+ " where a.GAP_User = ? and a.GAP_OWNER=?";
			return CM.getDao().getModule("根据用户查询列表和网闸节点查询实体", sql, GapOwnerConf.class, new Object[]{gapUser,owner});
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	

	/**
	 * 根据用户编码，网闸节点编码删除 配置关系
	 * @param gapUser
	 * @param owner
	 * @return
	 * @throws DaoException
	 */
	public boolean deleteByUserAndOwner(String gapUser,String owner) throws DaoException {
		String msg = "";
		List<Object[]> args=new ArrayList<Object[]>();
		args.add(new Object[]{OPENCLOSE.COLSE.getStatus(),gapUser,owner});
		try {
			String sql = "update  "
					+ SI_TABLE(" turbo_gap_owner_conf  a ")
					+ " set status=? where a.GAP_USER=? and a.GAP_OWNER=?";
			CM.getDao().batchUpdate("删除", sql, args);
		} catch (DaoException e) {
			msg = "批量更新数据发送状态失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
		return true;
	}
	/**
	 * 根据网闸节点编码删除 配置关系
	 * @param gapUser
	 * @param owner
	 * @return
	 * @throws DaoException
	 */
	public boolean deleteByOwner(String owner) throws DaoException {
		try {
			String sql = "delete from "
					+ SI_TABLE(" turbo_gap_owner_conf  a ")
					+ " where  a.GAP_OWNER=?";
			CM.getDao().delete("根据所有者删除网闸权限信息", sql, new Object[]{owner});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
		return true;
	}

}
